package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.panshi.modules.domain.QaAnswer;
import com.panshi.modules.domain.QaAnswerComment;
import com.panshi.modules.domain.ThRaiseDown;
import com.panshi.modules.mapper.QaAnswerCommentMapper;
import com.panshi.modules.mapper.QaAnswerMapper;
import com.panshi.modules.mapper.ThRaiseDownMapper;
import com.panshi.modules.service.ThRaiseDownService;
import com.panshi.vo.SaveOptionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyh
 * @since 2020-08-03
 */
@Service
public class ThRaiseDownServiceImpl extends ServiceImpl<ThRaiseDownMapper, ThRaiseDown> implements ThRaiseDownService {

    @Resource
    ThRaiseDownMapper thRaiseDownMapper;
    @Resource
    QaAnswerMapper qaAnswerMapper;
    @Resource
    QaAnswerCommentMapper qaAnswerCommentMapper;

    @Override
    public int optionVO(SaveOptionVO saveOptionVO) {
        if (StringUtils.isEmpty(saveOptionVO.getUserId())){
            return 0;
        }else
        if (StringUtils.isEmpty(saveOptionVO.getTId())){
            return 0;
        }else
        if (StringUtils.isEmpty(saveOptionVO.getType())){
            return 0;
        }

        //查询一下改用户是否点踩过了
        QueryWrapper<ThRaiseDown> thRaiseDownQueryWrapper = new QueryWrapper<>();
        thRaiseDownQueryWrapper
                .eq("user_id", saveOptionVO.getUserId())
                .eq("t_id", saveOptionVO.getTId())
                .eq("type", saveOptionVO.getType());
        ThRaiseDown raiseDown = thRaiseDownMapper.selectOne(thRaiseDownQueryWrapper);

        if (!StringUtils.isEmpty(raiseDown)){//查询结果不为空，以及点踩过了
            return -1;
        }

        //类型,10-资讯评论,20-问答回答,30-回答评论
        if (saveOptionVO.getType()==20){
            UpdateWrapper<QaAnswer> qaAnswerUpdateWrapper = new UpdateWrapper<>();
            //动作:1-点赞,0-点踩
            if (saveOptionVO.getPattern()==1){
                //修改回答点赞了
                qaAnswerUpdateWrapper
                        .eq("id", saveOptionVO.getTId())
                        .setSql(" raise_num = raise_num+1");
            }else{
                //修改回答点踩了
                qaAnswerUpdateWrapper
                        .eq("id", saveOptionVO.getTId())
                        .setSql(" down_num = down_num+1");

            }
            QaAnswer qaAnswer = new QaAnswer();
            qaAnswer.setId(saveOptionVO.getTId());
            qaAnswer.setUserId(saveOptionVO.getUserId());
            qaAnswerMapper.update(qaAnswer,qaAnswerUpdateWrapper);
        }else if(saveOptionVO.getType()==30){
            UpdateWrapper<QaAnswerComment> qaAnswerCommentUpdateWrapper = new UpdateWrapper<>();
            QaAnswerComment qaAnswerComment = new QaAnswerComment();
            //修改回答点踩了
            qaAnswerCommentUpdateWrapper
                    .eq("id", saveOptionVO.getTId())
                    .setSql("raise_num = raise_num+1");
            qaAnswerComment.setUserId(saveOptionVO.getUserId());
            qaAnswerCommentMapper.update(qaAnswerComment,qaAnswerCommentUpdateWrapper);
        }

        ThRaiseDown thRaiseDown = getQaAnswerUncheck(saveOptionVO);
        //点踩记录表增加数据
        int insert = thRaiseDownMapper.insert(thRaiseDown);

        return insert;
    }


    // ThRaiseDownVO 转 ThRaiseDown
    public ThRaiseDown getQaAnswerUncheck(SaveOptionVO saveOptionVO){
        ThRaiseDown thRaiseDown = new ThRaiseDown();
        if(saveOptionVO == null){
            return null;
        }
        BeanUtils.copyProperties(saveOptionVO,thRaiseDown);
        return thRaiseDown;
    }
}
